﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Resources;

namespace University.Workshop.WinApp
{
    public partial class SearchClientSimple :TemplateForm
    {
        Boolean isFromOtherForm = false;
        private ResourceManager resours;

        public SearchClientSimple()
        {
            InitializeComponent();
        }
        public SearchClientSimple(string formTitle)
        {
            InitializeComponent();
            this.Text = formTitle;
            this.isFromOtherForm = true;
            resours = new ResourceManager(typeof(CheckInForm));
        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {
            string fullname = string.Empty;
            string id = string.Empty;
            string telephone = string.Empty;

            fullname = textBoxNombre.Text;
            //id = textBoxCarnet.Text;
            //telephone = textBoxTelefono.Text;

            DataTable dtb = new DataTable();
            dtb = ClientSearcher.Search(fullname, id, telephone);


            try
            {
                try
                {
                    this.GridViewBuscar.DataSource = dtb;

                    labelInfo.Text = String.Format("Cantidad de registros encontrados: {0}", dtb.Rows.Count);
                }
                catch (Exception ex)
                {
                    labelInfo.Text = "Error: " + ex.Message;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("ERROR: " + ex.Message, " Comprobar tabla");
            }
        }

        private void GridViewBuscar_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (isFromOtherForm)
            {
                if (!(this.GridViewBuscar.Rows.Count - 1 > 0) && !(this.GridViewBuscar.SelectedRows.Count > 0))
                {
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "Row Selection Is Invalid", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                }
                else
                {
                    DataGridViewCell selectUserCi = this.GridViewBuscar.CurrentRow.Cells["ClientId"];
                    string ciSelect = selectUserCi.Value.ToString();
                    Program.ciClient = ciSelect;
                    this.Close();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

